package com.teradata.dashboard.service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.teradata.dashboard.constant.DashboardConstant;

import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class KpiDataService extends IntentService {

	

	private String lastUpdateTime;
	private boolean autoLoad;
	private Map<String,String> KpiData;
	
	private KpiDataBinder binder=new KpiDataBinder();
	
	public class KpiDataBinder extends Binder{
		public Map getKpiData(){
			return KpiData;
		}
		public String getLastUpdateTime(){
			return lastUpdateTime;
		}
		public boolean isAutoLoad() {
			return autoLoad;
		}
		public void setAutoLoad(boolean isautoLoad) {
			autoLoad = isautoLoad;
		}
	}
	public KpiDataService(String name) {
		super(name);		
	}
	
	public KpiDataService() {
		super("KpiDataService");		
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("onBind");
		return binder;
	}
	@Override
	protected void onHandleIntent(Intent intent) {
		System.out.println("onHandleIntent");
		//System.out.println(intent.getStringExtra("categoryNmae"));
		Random random = new Random();
		while(autoLoad){
			try{
				KpiData=new HashMap();
				KpiData.put(DashboardConstant.KPI_DATAITEM_VALUE, String.valueOf(random.nextInt(1000)));
				KpiData.put(DashboardConstant.KPI_DATAITEM_LAST_FREQ_VALUE, String.valueOf(random.nextInt(200)));
				KpiData.put(DashboardConstant.KPI_DATAITEM_LAST_YEAR_VALUE, String.valueOf(random.nextInt(400)));
				KpiData.put(DashboardConstant.KPI_DATAITEM_LAST_YEAR_END_VALUE, String.valueOf(random.nextInt(2000)));
				lastUpdateTime=String.valueOf(System.currentTimeMillis());
				Thread.sleep(1000*20);
			}catch (Exception e){
				
			}
		}
	}
	
	@Override
	public void onCreate(){
		super.onCreate();
		System.out.println("onCreate");
		autoLoad=getSharedPreferences("com.teradata.dashboard",MODE_PRIVATE).getBoolean(DashboardConstant.AUTO_LOAD, true);		
		System.out.println(autoLoad);
		
	}
	
	
	

}
